import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.openymsg.legacy.network.ContactListType;
import org.openymsg.legacy.network.Session;
import org.openymsg.legacy.network.Status;
import org.openymsg.legacy.network.YahooUser;
import org.openymsg.legacy.network.event.SessionAdapter;
import org.openymsg.legacy.network.event.SessionEvent;
import org.openymsg.legacy.network.event.SessionListEvent;

public class MyJMsg extends JFrame implements WindowFocusListener
{
	private static final long serialVersionUID = 9147187262264247815L;
	JPanel c;
	Session sesiune;
	JTextArea mesaje;
	JTextField id, chatToField;
	JPasswordField parola;
	JList friends;
	JScrollPane pane;
	DefaultListModel model;
	String chatToId;
	JLabel showChatToId, infoText;

	public MyJMsg()
	{
		super("My Java yahoo messenger client");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(40, 40, 550, 350);
		c = new JPanel();
		c.setBounds(0, 0, 550, 350);
		c.setLayout(null);
		getContentPane().add(c);
		sesiune = new Session();
		mesaje = new JTextArea();
		mesaje.setEditable(false);
		JScrollPane mesajeScrollPane = new JScrollPane(mesaje);
		mesajeScrollPane.setBounds(10, 70, 300, 200);
		c.add(mesajeScrollPane);
		chatToId = new String("");
		chatToField = new JTextField();
		chatToField.setBounds(180, 280, 300, 25);
		chatToField.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ev)
			{
				if (!chatToId.equals("") )
				{
					try
					{
						sesiune.sendMessage(chatToId, chatToField.getText());
					}
					catch (Exception e)
					{
						System.out.println("Mesajul+nu+a+fost+trimis!");
					}
					mesaje.append("-> OUT " + chatToId + " : "
							+ chatToField.getText() + "\n");
					chatToField.setText("");
				}
			}
		});
		c.add(chatToField);
		showChatToId = new JLabel();
		showChatToId.setText(chatToId + " :");
		showChatToId.setBounds(10, 280, 170, 25);
		c.add(showChatToId);
		pane = new JScrollPane();
		pane.setBounds(330, 70, 150, 200);
		model = new DefaultListModel();
		friends = new JList(model);
		friends.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent ev) {
				JList aux = (JList) ev.getSource();
				Object[] selectari = aux.getSelectedValues();
				MyYahooUser currentMyYahooUser = (MyYahooUser) selectari[0];
				chatToId = currentMyYahooUser.getYahooUser().getId();
				showChatToId.setText(currentMyYahooUser.toString() + " :");
			}
		});
		pane.getViewport().add(friends);
		c.add(pane);
		infoText = new JLabel("ID:");
		infoText.setBounds(130, 10, 20, 25);
		c.add(infoText);
		id = new JTextField();
		id.setBounds(160, 10, 100, 25);
		c.add(id);
		infoText = new JLabel("Pass:");
		infoText.setBounds(270, 10, 40, 25);
		c.add(infoText);
		parola = new JPasswordField();
		parola.setBounds(320, 10, 100, 25);
		c.add(parola);
		JButton buton = new JButton("Login");
		buton.setBounds(10, 10, 100, 25);
		buton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				try {
					sesiune.setStatus(Status.INVISIBLE);
					sesiune.addSessionListener(new ProcEv());
					sesiune.login(id.getText(),
							new String(parola.getPassword()));
					System.out.println("Login+SUCCEDED.");
				} catch (Exception e) {
					System.out.println("Login+FAILED: " + e.getMessage());
				}
			}
		});
		c.add(buton);
		buton = new JButton("Logout");
		buton.setBounds(10, 40, 100, 25);
		buton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				try {
					sesiune.logout();
					System.out.println("Logout+succeded.");
				} catch (Exception e) {
					System.out.println("Logout+FAILED: " + e);
				}
				System.exit(0);
			}
		});
		c.add(buton);
		addWindowFocusListener(this);
		setVisible(true);
		System.out.println("Constructed+");
	}

	public static void main(String[] params)
	{
		MyJMsg chatObj = new MyJMsg();
		System.out.println("static+void+main+here!");
	}

	private class MyYahooUser
	{
		private YahooUser yahooUser;

		public MyYahooUser(YahooUser yahooUser)
		{
			this.yahooUser = yahooUser;
		}

		public YahooUser getYahooUser()
		{
			return yahooUser;
		}

		@Override
		public String toString()
		{
			return (yahooUser.isLoggedIn() ? "[On] " : "[Off] ") + yahooUser.getId();
		}
	}

	private class ProcEv extends SessionAdapter
	{
		@Override
		public void messageReceived(SessionEvent msg) {
			String mesaj = new String(msg.getMessage());
			final String deLaId = new String(msg.getFrom());
			mesaje.append("<- IN " + deLaId + " : " + mesaj + "\n");
			SwingUtilities.invokeLater( new Runnable()
			{
				@Override
				public void run( )
				{
					MyJMsg.this.setTitle("NEW message from: " + deLaId);
				}
			});
			System.out.println("mesaj+online: " + mesaj);
		}

		@Override
		public void listReceived(SessionListEvent lista) {
			System.out.println("type+lista: " + lista.getType() );
			if (lista.getType() == ContactListType.Friends)
			{
				Set<YahooUser> listaPrieteni = lista.getContacts();
				for (YahooUser currentYahooUser : listaPrieteni)
				{
					if (currentYahooUser.isFriend() )
					{
						final MyYahooUser aux = new MyYahooUser(currentYahooUser);
						SwingUtilities.invokeLater( new Runnable()
						{
							@Override
							public void run( )
							{
								model.addElement(aux);
							}
						});
					}
				}
				MyJMsg.this.validate();
			}
		}

		@Override
		public void offlineMessageReceived(SessionEvent mesajOffline) {
			String mesaj = new String(mesajOffline.getMessage() );
			String delaId = new String(mesajOffline.getFrom() );
			mesaje.append("<- offline " + delaId + " : " + mesaj + "\n");
			System.out.println("mesaj+offline: " + mesaj);
		}
	}

	@Override
	public void windowGainedFocus(WindowEvent e) {
		SwingUtilities.invokeLater( new Runnable()
		{
			@Override
			public void run( )
			{
				setTitle("My Java yahoo messenger client");
			}
		});
		System.out.println("focus+gained");
	}

	@Override
	public void windowLostFocus(WindowEvent e) {
		SwingUtilities.invokeLater( new Runnable()
		{
			@Override
			public void run( )
			{
				setTitle("Waiting for new message...");
			}
		});
		System.out.println("focus+lost");
	}

	private void invokeLater()
	{
		
	}
}